package com.tianji.learning.controller;


import com.tianji.common.domain.dto.PageDTO;
import com.tianji.common.exceptions.BadRequestException;
import com.tianji.common.utils.BeanUtils;
import com.tianji.learning.dto.QuestionFormDTO;
import com.tianji.learning.dto.QuestionUpdateDTO;
import com.tianji.learning.entity.InteractionQuestion;
import com.tianji.learning.query.QuestionAdminPageQuery;
import com.tianji.learning.query.QuestionPageQuery;
import com.tianji.learning.service.IInteractionQuestionService;
import com.tianji.learning.vo.QuestionAdminVO;
import com.tianji.learning.vo.QuestionVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 * 互动提问的问题表 前端控制器
 * </p>
 *
 * @author author
 * @since 2023-05-29
 */
@RestController
@RequestMapping("/admin/questions")
@RequiredArgsConstructor
@Api("管理端：互动提问的问题表")
@Slf4j
public class InteractionQuestionAdminController {

    private final IInteractionQuestionService questionService;

    @ApiOperation("根据id查询问题详情")
    @GetMapping("/{id}")
    public QuestionAdminVO getInfoAdmin(@PathVariable("id") Long id){
        return questionService.getInfoAdmin(id);
    }

    @ApiOperation("分页查询互动问题")
    @GetMapping("page")
    public PageDTO<QuestionAdminVO> queryQuestionPage(QuestionAdminPageQuery query){
        return questionService.queryQuestionAdminPage(query);
    }

    @ApiOperation("新增提问")
    @PostMapping
    public void saveQuestion(@Valid @RequestBody QuestionFormDTO questionDTO){
        log.info("新增提问：{}",questionDTO);
        questionService.saveQuestion(questionDTO);
    }

    @ApiOperation("隐藏问题")
    @PutMapping("/{id}/hidden/{hidden}")
    public void updateQuestion(@PathVariable("id") Long id, @PathVariable("hidden") Boolean hidden){
        log.info("是否隐藏提问：{}",hidden);
        questionService.lambdaUpdate()
                .set(InteractionQuestion::getHidden,hidden)
                .eq(InteractionQuestion::getId,id)
                .update();
    }

    @ApiOperation("删除提问")
    @DeleteMapping("/{id}")
    public void deleteQuestion(@PathVariable("id") Long id){
        if (id == null) {
            throw new BadRequestException("id不能为空");
        }
        log.info("删除提问：{}",id);
        questionService.removeById(id);
    }

}
